package com.dog.basics.netty.business.impl;

import cn.hutool.core.util.StrUtil;
import com.dog.basics.bean.dto.api.mtkj.ApiEndOrderAddDto;
import com.dog.basics.common.util.SpringContextUtils;
import com.dog.basics.netty.business.IBusinessServer;
import com.dog.basics.netty.protobuf.dto.DogNewsContentDto;
import com.dog.basics.netty.util.SessionSocketHolder;
import com.dog.basics.service.mtkj.IMtDeviceOrderService;
import io.netty.channel.ChannelHandlerContext;
import lombok.extern.log4j.Log4j2;

/**
 * @description: 归还轮椅
 * @author: He
 * @date: Created in 2020/10/12 12:18
 * @version: 1.0
 * @modified By:
 */
@Log4j2
public class ResponseHandlerImplReturnWheelchair implements IBusinessServer<DogNewsContentDto.Message> {

    /**
     * @功能描述: 归还轮椅 - 执行
     * @author He
     * @date 2020/10/12
     */
    @Override
    public void businessHandler(ChannelHandlerContext ctx, DogNewsContentDto.Message msg) throws InterruptedException {

        // 执行规范轮椅操作
        Long dvId = SessionSocketHolder.getSession(ctx.channel().id().asLongText());
        log.info("归还轮椅:{} 锁通道号 : {} ", msg.toString(),dvId);
        ApiEndOrderAddDto build = ApiEndOrderAddDto.builder().lockId(dvId).passNum(StrUtil.toString(msg.getLockport())).orderDeviceWheelchairId(msg.getIDcard()).build();
        Boolean dataReturn = Boolean.FALSE;
        try {
            dataReturn = SpringContextUtils.getBean(IMtDeviceOrderService.class).endOrder(build);
        } catch (Exception e) {
            log.error("归还轮椅-异常:{} 归还轮椅:{} 锁通道号 : {}", e.getMessage(),msg.toString(),dvId);
        }
        // 回应信息
        log.info("归还轮椅:{} 锁通道号 : {} 回应标识 : {} ", msg.toString(),dvId,dataReturn ? 1 : 2);
        ctx.channel().writeAndFlush(DogNewsContentDto.Message.newBuilder().setMsgType(msg.getMsgType()).setMsgCode(dataReturn ? 1 : 2).build());
    }

}
